<?php

@session_start();

extract($_REQUEST);

require_once "../lib/config.php";
require_once "../lib/functions.php";
require_once "../lib/pdomanager.php";
require_once "../lib/paginate_class.php";

$pdodbobj=new DB();
$pdodbobj->dbname=DBNAME;
$pdodbobj->hostname=HOST;
$pdodbobj->dbusername=USERNAME;
$pdodbobj->dbpassword=PASSWORD;
$pdodbobj->dbtype=TYPE;
$pdodbobj->portnumber=PORTNUMBER;

$starcount=1;

$connstatus=$pdodbobj->connect();

if($action=="saverating" && $userid>0 && $starrating>0 && $appid>0)
{
    $selecquery="select id from af_apps_ratings where app_id=:appid and user_id=:userid ";
    $existsrecord= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid,":userid"=>$userid)));
    if(count($existsrecord)>0)
    {
        echo "Already";
    }
    else
    {
        $insertquery="insert into af_apps_ratings set app_id=:appid,user_id=:userid,rating=:rating,rate_date='".time()."' ";
        $pdodbobj->query($insertquery,array(":appid"=>$appid,":userid"=>$userid,":rating"=>$starrating));
        if($pdodbobj->affectedrows>0)
        {
            $selecquery="select avg(rating) as avgrating from af_apps_ratings where app_id=:appid ";
            $averagerating= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid)));
            if(count($averagerating)>0)
            {
                $updatequery="update af_apps set average_rating='".$averagerating[0]['avgrating']."' where id=:appid";
                $pdodbobj->query($updatequery,array(":appid"=>$appid));
                echo round($averagerating[0]['avgrating']);
            }
        }
    }
}

if($action=="pickupdate" && $userid>0 && $appid>0)
{
    //$selecquery="select id from af_pics where app_id=:appid and user_id=:userid ";
//    $existsrecord= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid,":userid"=>$userid)));
//    if(count($existsrecord)>0)
//    {
//        echo "Already";
//    }
//    else
//    {
        $insertquery="insert into af_pics set app_id=:appid,user_id=:userid,pick_date='".time()."' ";
        $pdodbobj->query($insertquery,array(":appid"=>$appid,":userid"=>$userid));
        if($pdodbobj->affectedrows>0)
        {
            $updatequery="update af_apps set total_pics=total_pics+1 where id=:appid";
            $pdodbobj->query($updatequery,array(":appid"=>$appid));
            
            $selecquery="select total_pics from af_apps where id=:appid ";
            $pickcount= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid)));
            
            echo $pickcount[0]['total_pics'];
        }
    //}
}

if($action=="getcategories" && $lowerlimit>0 && $nextcount>0)
{
    $selecquery="select id,cat_name from af_categories where type='Games' and cat_status='Active' order by cat_name limit ".$lowerlimit.", ".$nextcount;
    $allcategories= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array()));
    
    for($i=0;$i<count($allcategories);$i++)
    {
        ?>
        <li>
            <span><a href="javascript:void(0);" onclick="return getcategoryapps(<?php echo $allcategories[$i]['id']; ?>);"> <?php echo $allcategories[$i]['cat_name']; ?> </a></span>
        </li>
        <?php
    }
                        
}


if($action=="getwebapps" && $nextcount>0 )
{
    $condition=array();
    $query="select id,app_name,image_url,app_link,small_description,total_pics,average_rating from af_apps where app_status='Active' ";
    $serachcond="";
    if(isset($searchcatid) && $searchcatid>0)
    {
        $condition[":catid"]=$searchcatid;
        $serachcond.=" and cat_id=:catid ";
    }
    
    if(isset($appname) && trim($appname)!="")
    {
        $condition[":appname"]="%".$appname."%";
        $serachcond.=" and app_name like :appname ";
    }
    
    if(!isset($ordertype))
    {
        $ordertype="asc";
    }
    
    $webquery=$query." and app_type='Web' ".$serachcond." order by app_name ".$ordertype." limit ".$lowerlimit.", ".$nextcount;
    
    $webapps=$pdodbobj->fetch_array($pdodbobj->query($webquery,$condition));    
    
    $starcount=$nextstarcount;
    if(count($webapps)>0)
    {
        for($j=0;$j<count($webapps);$j++)
        {
            if(!(strpos($webapps[$j]['app_link'],"http://")>=0))
            {
                $webapps[$j]['app_link']="http://".$webapps[$i]['app_link'];
            }
            if(($j+1)%2==0)
            {
                ?>
                <tr>
                    <td style="padding-top:20px;" valign="top">
    					<div class="chanel_pic2">
                            <a href="javascript:void(0);" onclick="return updatePickcounter(<?php echo $webapps[$j]['id']; ?>,<?php echo $userid; ?>,'<?php echo $webapps[$j]['app_link']; ?>',<?php echo $starcount; ?>);">
                                <img src="<?php echo "uploads/apps/".$webapps[$j]['image_url']; ?>" width="166px" height="116px" />
                            </a>
    					</div>
    					<div class="star2">
    					<p class="ch_name"><a href="javascript:void(0);" onclick="updatePickcounter(<?php echo $webapps[$j]['id']; ?>,<?php echo $userid; ?>,'<?php echo $webapps[$j]['app_link']; ?>',<?php echo $starcount; ?>);"><?php echo $webapps[$j]['app_name']; ?></a></p>
    					<p class="ch_name">Rating</p>
    					<p class="ret"><div id="star<?php echo $starcount; ?>"></div>
                            <input type="hidden" name="gameid<?php echo $starcount; ?>" id="gameid<?php echo $starcount; ?>" value="<?php echo $webapps[$j]['id']; ?>" />
                            <input type="hidden" name="avgrating<?php echo $starcount; ?>" id="avgrating<?php echo $starcount; ?>" value="<?php echo round($webapps[$j]['average_rating']); ?>" />
                        </p>
    					<p class="ret">pick<span class="num" id="pick<?php echo $starcount; ?>"><?php echo $webapps[$j]['total_pics']; ?></span></p>
    					<p class="ret"><?php echo $webapps[$j]['small_description']; ?></p>
    					
    					</div>
    					
    				</td>
                </tr>
                <?php
            }
            else
            {
                ?>
                <tr>
                    <td>
                        <div class="chanel_pic">
                            <a href="javascript:void(0);" onclick="return updatePickcounter(<?php echo $webapps[$j]['id']; ?>,<?php echo $userid; ?>,'<?php echo $webapps[$j]['app_link']; ?>',<?php echo $starcount; ?>);">
                                <img src="<?php echo "uploads/apps/".$webapps[$j]['image_url']; ?>" width="166px" height="116px" />
                            </a>
    					</div>
    					<div class="star">
    					<p class="ch_name"><a href="javascript:void(0);" onclick="updatePickcounter(<?php echo $webapps[$j]['id']; ?>,<?php echo $userid; ?>,'<?php echo $webapps[$j]['app_link']; ?>',<?php echo $starcount; ?>);"><?php echo $webapps[$j]['app_name']; ?></a></p>
    					<p class="ch_name">Rating</p>
    					<p class="ret">
                            <div id="star<?php echo $starcount; ?>"></div>
                            <input type="hidden" name="gameid<?php echo $starcount; ?>" id="gameid<?php echo $starcount; ?>" value="<?php echo $webapps[$j]['id']; ?>" />
                            <input type="hidden" name="avgrating<?php echo $starcount; ?>" id="avgrating<?php echo $starcount; ?>" value="<?php echo round($webapps[$j]['average_rating']); ?>" />
                        </p>
    					<p class="ret">pick<span class="num" id="pick<?php echo $starcount; ?>"><?php echo $webapps[$j]['total_pics']; ?></span></p>
    					<p class="ret"><?php echo $webapps[$j]['small_description']; ?></p>
    					
    					</div>	
    					
                    </td>
                </tr>
                <?php
            }
            $starcount++;    
        } 
        echo "~:~".$starcount;
    }
    else
    {
        if(isset($newflag) && $newflag==1)
        {
            ?>
            <tr>
                <td valign="top">
                    <span style="color: red;">
                        There is not app found of this category.
                    </span>
                </td>
            </tr>
            <?php
            echo "~:~0";
        }
    }             
}

if($action=="getmobileapps" && $nextcount>0 )
{
    $condition=array();
    $query="select id,app_name,image_url,app_link,small_description,total_pics,average_rating from af_apps where app_status='Active' ";
    $serachcond="";
    
    if(!isset($ordertype))
    {
        $ordertype="asc";
    }
    
    if(isset($searchcatid) && $searchcatid>0)
    {
        $condition[":catid"]=$searchcatid;
        $serachcond.=" and cat_id=:catid ";
    }
    
    if(isset($appname) && trim($appname)!="")
    {
        $condition[":appname"]="%".$appname."%";
        $serachcond.=" and app_name like :appname ";
    }
    
    $mobilequery=$query." and app_type='Mobile' ".$serachcond." order by app_name ".$ordertype." limit ".$lowerlimit.", ".$nextcount;
    $mobileapps=$pdodbobj->fetch_array($pdodbobj->query($mobilequery,$condition));    
    
    if($starcount==1)
    {
        $starcount=$nextstarcount;
    }
    
    if(count($mobileapps)>0)
    {
        for($j=0;$j<count($mobileapps);$j++)
        {
            if(!(strpos($mobileapps[$j]['app_link'],"http://")>=0))
            {
                $mobileapps[$j]['app_link']="http://".$mobileapps[$i]['app_link'];
            }
            if(($j+1)%2!=0)
            {
                ?>
                <tr>
                    <td valign="top">
    					<div class="chanel_pic2">
                            <a href="javascript:void(0);" onclick="return updatePickcounter(<?php echo $mobileapps[$j]['id']; ?>,<?php echo $userid; ?>,'<?php echo $mobileapps[$j]['app_link']; ?>',<?php echo $starcount; ?>);">
                                <img src="<?php echo "uploads/apps/".$mobileapps[$j]['image_url']; ?>" width="166px" height="116px" />
                            </a>
    					</div>
    					<div class="star2">
    					<p class="ch_name"><a href="javascript:void(0);" onclick="updatePickcounter(<?php echo $mobileapps[$j]['id']; ?>,<?php echo $userid; ?>,'<?php echo $mobileapps[$j]['app_link']; ?>',<?php echo $starcount; ?>);"><?php echo $mobileapps[$j]['app_name']; ?></a></p>
    					<p class="ch_name">Rating</p>
    					<p class="ret"><div id="star<?php echo $starcount; ?>"></div>
                            <input type="hidden" name="gameid<?php echo $starcount; ?>" id="gameid<?php echo $starcount; ?>" value="<?php echo $mobileapps[$j]['id']; ?>" />
                            <input type="hidden" name="avgrating<?php echo $starcount; ?>" id="avgrating<?php echo $starcount; ?>" value="<?php echo round($mobileapps[$j]['average_rating']); ?>" />
                        </p>
    					<p class="ret">pick<span class="num" id="pick<?php echo $starcount; ?>"><?php echo $mobileapps[$j]['total_pics']; ?></span></p>
    					<p class="ret"><?php echo $mobileapps[$j]['small_description']; ?></p>
    					
    					</div>
    					
    				</td>
                </tr>
                <?php
            }
            else
            {
                ?>
                <tr>
                    <td style="padding-top:20px;">
                        <div class="chanel_pic">
                            <a href="javascript:void(0);" onclick="return updatePickcounter(<?php echo $mobileapps[$j]['id']; ?>,<?php echo $userid; ?>,'<?php echo $mobileapps[$j]['app_link']; ?>',<?php echo $starcount; ?>);">
                                <img src="<?php echo "uploads/apps/".$mobileapps[$j]['image_url']; ?>" width="166px" height="116px" />
                            </a>
    					</div>
    					<div class="star">
    					<p class="ch_name"><a href="javascript:void(0);" onclick="updatePickcounter(<?php echo $mobileapps[$j]['id']; ?>,<?php echo $userid; ?>,'<?php echo $mobileapps[$j]['app_link']; ?>',<?php echo $starcount; ?>);"><?php echo $mobileapps[$j]['app_name']; ?></a></p>
    					<p class="ch_name">Rating</p>
    					<p class="ret">
                            <div id="star<?php echo $starcount; ?>"></div>
                            <input type="hidden" name="gameid<?php echo $starcount; ?>" id="gameid<?php echo $starcount; ?>" value="<?php echo $mobileapps[$j]['id']; ?>" />
                            <input type="hidden" name="avgrating<?php echo $starcount; ?>" id="avgrating<?php echo $starcount; ?>" value="<?php echo round($mobileapps[$j]['average_rating']); ?>" />
                        </p>
    					<p class="ret">pick<span class="num" id="pick<?php echo $starcount; ?>"><?php echo $mobileapps[$j]['total_pics']; ?></span></p>
    					<p class="ret"><?php echo $mobileapps[$j]['small_description']; ?></p>
    					
    					</div>	
    					
                    </td>
                </tr>
                <?php
            }
            $starcount++;    
        }
        echo "~:~".$starcount; 
    }
    else
    {
        if(isset($newflag) && $newflag==1)
        {
        ?>
            <tr>
                <td valign="top">
                    <span style="color: red;">
                        There is not app found of this category.
                    </span>
                </td>
            </tr>
            <?php
            echo "~:~0";
        }
    }              
}

?>